package com.grablist.feihu.grablist.ui.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.grablist.feihu.grablist.util.LogUtils;


/**
 * Created by Administrator on 2017/10/18.
 */

public class ListViewHeight {
    public static void setListViewHeightBasedOnChildren(ListView listView, int num, Context context,int height) {
//        ListAdapter listAdapter = listView.getAdapter();
//        if (listAdapter == null) {
//            return;
//        }
//        int totalHeight = 0;
////        for (int i = 0; i < listAdapter.getCount(); i++) {
////            View listItem = listAdapter.getView(i, null, listView);
////            listItem.measure(0, 0);
////            totalHeight += listItem.getMeasuredHeight();
////
////            LogUtils.Debug_E("aa",":"+totalHeight);
////        }
//        if(listAdapter.getCount()>4){
//            View listItem = listAdapter.getView(0, null, listView);
//            listItem.measure(0, 0);
//            totalHeight = listItem.getMeasuredHeight()*4;
//        }
//
//
//        ViewGroup.LayoutParams params = listView.getLayoutParams();
//        params.height = totalHeight + (listView.getDividerHeight() *4);
//        LogUtils.Debug_E("aa",":::"+params.height);
//        listView.setLayoutParams(params);

        if (num > 3){
            ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);

//            int height = wm.getDefaultDisplay().getHeight();
            layoutParams.height = height;
            listView.setLayoutParams(layoutParams);
        }else{
            ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();


        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight+60;
            LogUtils.d(params.height+":::");
            listView.setLayoutParams(params);
        }
    }
}
